package com.yanfan.zutai.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.yanfan.common.core.domain.entity.SysDept;
import com.yanfan.common.core.domain.model.LoginUser;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.iot.domain.Device;
import com.yanfan.system.service.ISysDeptService;
import com.yanfan.zutai.domain.BDeviceVideo;
import com.yanfan.zutai.domain.vo.BVideoToken;
import com.yanfan.zutai.mapper.BDeviceVideoMapper;
import com.yanfan.zutai.service.IBDeviceManagerService;
import com.yanfan.zutai.service.IBDeviceVideoService;
import com.yanfan.zutai.util.Detect;
import com.yanfan.zutai.util.HttpPostAndGetUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 视频监控Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-06
 */
@Service
public class BDeviceVideoServiceImpl implements IBDeviceVideoService {
    @Autowired
    private BDeviceVideoMapper bDeviceVideoMapper;
    @Autowired
    private IBDeviceManagerService bDeviceManagerService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询视频监控
     *
     * @param id 视频监控主键
     * @return 视频监控
     */
    @Override
    public BDeviceVideo selectBDeviceVideoById(Long id) {
        return bDeviceVideoMapper.selectBDeviceVideoById(id);
    }

    /**
     * 查询视频监控列表
     *
     * @param bDeviceVideo 视频监控
     * @return 视频监控
     */
    @Override
    public List<BDeviceVideo> selectBDeviceVideoList(BDeviceVideo bDeviceVideo) {
        return bDeviceVideoMapper.selectBDeviceVideoList(bDeviceVideo);
    }

    /**
     * 查询视频监控列表
     *
     * @param ztGuid 组态编号
     * @return 视频监控
     */
    @Override
    public List<Device> selectBDeviceVideoLists(String ztGuid) {
        return bDeviceVideoMapper.selectBDeviceVideoLists(ztGuid);
    }

    /**
     * 新增视频监控
     *
     * @param bDeviceVideo 视频监控
     * @return 结果
     */
    @Override
    public int insertBDeviceVideo(BDeviceVideo bDeviceVideo) {
        Snowflake snowflake = IdUtil.getSnowflake(6, 2);
        long id = snowflake.nextId();
        bDeviceVideo.setId(id);
        bDeviceVideo.setCreateTime(DateUtils.getNowDate());
        BVideoToken videoToken = this.refreshToken(bDeviceVideo);
        if ("200".equals(videoToken.getCode())) {
            bDeviceVideo.setAccessToken(videoToken.getData().getAccessToken());
            bDeviceVideo.setExpireTime(videoToken.getData().getExpireTime());
        }
        return bDeviceVideoMapper.insertBDeviceVideo(bDeviceVideo);
    }

    /**
     * 修改视频监控
     *
     * @param bDeviceVideo 视频监控
     * @return 结果
     */
    @Override
    public int updateBDeviceVideo(BDeviceVideo bDeviceVideo) {
        bDeviceVideo.setUpdateTime(DateUtils.getNowDate());
        BVideoToken videoToken = this.refreshToken(bDeviceVideo);
        if ("200".equals(videoToken.getCode())) {
            bDeviceVideo.setAccessToken(videoToken.getData().getAccessToken());
            bDeviceVideo.setExpireTime(videoToken.getData().getExpireTime());
        }
        return bDeviceVideoMapper.updateBDeviceVideo(bDeviceVideo);
    }

    /**
     * 批量删除视频监控
     *
     * @param ids 需要删除的视频监控主键
     * @return 结果
     */
    @Override
    public int deleteBDeviceVideoByIds(Long[] ids) {
        return bDeviceVideoMapper.deleteBDeviceVideoByIds(ids);
    }

    /**
     * 删除视频监控信息
     *
     * @param id 视频监控主键
     * @return 结果
     */
    @Override
    public int deleteBDeviceVideoById(Long id) {
        return bDeviceVideoMapper.deleteBDeviceVideoById(id);
    }

    @Override
    public BVideoToken refreshToken(BDeviceVideo bDeviceVideo) {
        String url = "https://open.ys7.com/api/lapp/token/get?appKey=" + bDeviceVideo.getAppKey() + "&appSecret=" + bDeviceVideo.getAppSecret();
        return HttpPostAndGetUtil.doPostRefreshToken(url);
    }

    @Override
    public void refreshAllToken() {
        BDeviceVideo bDeviceVideo = new BDeviceVideo();
        bDeviceVideo.setExpireTime(new Date().getTime());
        List<BDeviceVideo> bDeviceVideoList = this.selectBDeviceVideoList(bDeviceVideo);
        for (int i = 0; i < bDeviceVideoList.size(); i++) {
            bDeviceVideo = bDeviceVideoList.get(i);
            BVideoToken videoToken = this.refreshToken(bDeviceVideo);
            if ("200".equals(videoToken.getCode())) {
                bDeviceVideo.setAccessToken(videoToken.getData().getAccessToken());
                bDeviceVideo.setExpireTime(videoToken.getData().getExpireTime());
                bDeviceVideo.setUpdateTime(new Date());
                this.updateBDeviceVideo(bDeviceVideo);
            }
        }
    }

    @Override
    public String importData(List<BDeviceVideo> deviceList) {
        int num = 0;
        LoginUser loginUser = SecurityUtils.getLoginUser();
        for (int i = 0; i < deviceList.size(); i++) {
            BDeviceVideo bDeviceVideo = deviceList.get(i);
            if (Detect.isEmpty(bDeviceVideo.getAppKey()) || Detect.isEmpty(bDeviceVideo.getAppSecret()) || Detect.isEmpty(bDeviceVideo.getChannelNumber()) || Detect.isEmpty(bDeviceVideo.getSerialNumber())) {
                continue;
            }
            SysDept sysDept = sysDeptService.selectDeptById(loginUser.getUser().getDeptId());
            String deptIds = "," + sysDept.getAncestors() + "," + sysDept.getDeptId() + ",";
            bDeviceVideo.setDeptIdStrs(deptIds);
            bDeviceVideo.setCreateBy(loginUser.getUsername());
            bDeviceVideo.setCreateTime(new Date());
            bDeviceVideo.setUpdateTime(new Date());
            bDeviceVideo.setUpdateBy(loginUser.getUsername());
            BVideoToken videoToken = this.refreshToken(bDeviceVideo);
            if ("200".equals(videoToken.getCode())) {
                bDeviceVideo.setAccessToken(videoToken.getData().getAccessToken());
                bDeviceVideo.setExpireTime(videoToken.getData().getExpireTime());
            }
            this.insertBDeviceVideo(bDeviceVideo);
            num++;
        }
        return "成功导入" + num + "条设备数据！";
    }
}
